<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="apple-itunes-app" content="app-id=468944728">
<meta id="viewport" name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,initial-scale=1.0,user-scalable=no">
<title>授权页</title>
<head>
<script type="text/javascript" src="../plug/jquery-easyui/jquery-1.8.0.min.js"></script>
<script>

var appid = '';
var secret = '';
var weixinId = getUrlParam("weixinId");
var url_ = 'http://site.zjwcwl.com/pages/memberCore.html?weixinId='+weixinId;
var redirect_uri = encodeURI(url_);
var code = '';

//根据微信ID，获取微信公众账号信息
$.post('http://site.zjwcwl.com/weixin/getWeixinConfigById?weixinId='+weixinId,function(data){
	
	appid = jQuery.trim(data.appId);
	secret = jQuery.trim(data.appSecret);
	
	code = getUrlParam("code");
	if(code==null){
		window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+"&redirect_uri="+redirect_uri+"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
	}else{
		//alert('appid:'+appid+',secret:'+secret+',code:'+code+',grant_type:authorization_code');
		//获取当前微信用户信息
		$.ajax({
			type:'post',
			dataType:'json',
			url:'http://site.zjwcwl.com/weixin/getWxUserInfo',
			data:{'appid':appid,'secret':secret,'code':code,'grant_type':'authorization_code'},
			async:false,
			success:function(data){
				var result = JSON.parse(data.result);
				if(result.errcode == 48001){
					window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+"&redirect_uri="+redirect_uri+"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
				}else{
	          		if(result.openid == undefined || result.openid == 'undefined'){
	          			window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+"&redirect_uri="+redirect_uri+"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
	          		}else{
	          			//alert("shopId="+shopId+",externalNo="+result.openid+",externalName="+result.nickname+",logo="+result.headimgurl+',shopId='+shopId);
	          			$.ajax({
		          			type:'post',
		          			dataType:'json',
		          			url:"http://site.zjwcwl.com/fuzion/member/getByOpenId",
		          			data:{'openId':result.openid},
		          			async:false,
		          			success:function(data){
		          				if(data!=null){
		          					//这里直接跳到微商城   也就是首页地址
		          					//alert('http://threedldsite.53xsd.com/wx/index?shopId='+shopId + "&memberId="+data.result);
					          		window.location.href = 'http://site.zjwcwl.com/weixin/member/memberCore';
						          }else{
						          	alert('系统异常1');
						          }
		          				
		          			},
		          			error:function(){
					           alert('系统异常2');
					         }
		          		});
	          		}
	          		
	          	}
          		
			},
			error:function(){
				alert('系统异常3');
			}
			
		})
	}
	
})


//地址栏获取参数
function getUrlParam(name){
	var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); 
	var r = window.location.search.substr(1).match(reg);  
	if (r!=null) 
		return unescape(r[2]); 
	return null; 
} 

</script>
</head>
<body></body>
</html>